<?php
// +----------------------------------------------------------------------
// | 编辑/添加功能区组件
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace addons\BuilderPlus\components;

class Edit
{
    protected $config = [
        'type'           => 2,
        'format'         => 'text',
        'dateRange'      => false,
        'value'          => [],
        'upload'         => '',
        'require'        => false,
        'maxNum'         => 1,
        'linkageUrl'     => '',
        'change'         => '',
        'condition'      => [],
        'conditionLogic' => '',
        'suffix'          => '',
        'suffixColor'     => ''
    ];

    public function type(int $type = 2)
    {
        $this->config['type'] = $type;
        return $this;
    }

    public function images(int $num = 1, string $url = '')
    {
        $this->config['format'] = 'images';
        $this->config['maxNum'] = $num;
        $this->config['upload'] = $url ?: url('uploads/image')->build();
        return $this;
    }

    public function video(int $num = 1, string $url = '')
    {
        $this->config['format'] = 'video';
        $this->config['maxNum'] = $num;
        $this->config['upload'] = $url ?: url('uploads/video')->build();
        return $this;
    }

    public function date(string $type = 'date', bool $range = true)
    {
        $this->config['format'] = $type;
        $this->config['dateRange'] = $range;
        return $this;
    }

    public function text()
    {
        $this->config['format'] = 'text';
        return $this;
    }

    public function textarea()
    {
        $this->config['format'] = 'textarea';
        return $this;
    }

    public function number()
    {
        $this->config['format'] = 'number';
        return $this;
    }

    public function linkage(string $url)
    {
        $this->config['format'] = 'linkage';
        $this->config['linkageUrl'] = $url;
        return $this;
    }

    public function password()
    {
        $this->config['format'] = 'password';
        return $this;
    }

    public function ratio()
    {
        $this->config['format'] = 'ratio';
        return $this;
    }

    public function checkbox(array $value)
    {
        $this->config['format'] = 'checkbox';
        $this->config['value'] = $value;
        return $this;
    }

    public function radio(array $value)
    {
        $this->config['format'] = 'radio';
        $this->config['value'] = $value;
        return $this;
    }

    public function select(array $value)
    {
        $this->config['format'] = 'select';
        $this->config['value'] = $value;
        return $this;
    }

    public function editor()
    {
        $this->config['format'] = 'editor';
        $this->config['upload'] = url('uploads/editor')->build();
        return $this;
    }

    public function dateRange(): self
    {
        $this->config['dateRange'] = true;
        return $this;
    }

    public function change(array $value)
    {
        $this->config['change'] = $value;
        return $this;
    }

    public function maxNum(int $maxNum = 1): self
    {
        $this->config['maxNum'] = $maxNum;
        return $this;
    }

    public function value(array $value): self
    {
        $this->config['value'] = $value;
        return $this;
    }

    public function suffix(string $suffix, string $color = ''): self
    {
        $this->config['suffix'] = $suffix;
        $this->config['suffixColor'] = $color;
        return $this;
    }

    public function require(): self
    {
        $this->config['require'] = true;
        return $this;
    }

    public function upload(string $upload): self
    {
        $this->config['upload'] = $upload;
        return $this;
    }

    public function condition(array $condition, string $logic = 'and')
    {
        $this->config['condition'] = $condition;
        $this->config['conditionLogic'] = $logic;
        return $this;
    }

    public function render()
    {
        return $this->config;
    }

    public function __call($name, $arguments)
    {
        if (isset($this->config[$name])) {
            $this->config[$name] = $arguments;
        }
        return $this;
    }
}